package com.example.reggie.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.entity.ShoppingCart;
import com.example.reggie.service.ShoppingCartService;
import com.example.reggie.utils.BaseContext;
import com.example.reggie.utils.R;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart SC = shoppingCartService.add(shoppingCart);
        return R.success(SC);
//        Long userId = BaseContext.getId();
//        Long dId;
//        if (shoppingCart.getSetmealId() != null){
//            dId =  shoppingCart.getSetmealId();
//        }else {
//            dId = shoppingCart.getDishId();
//        }
//        String json = JSON.toJSONString(one);
//        stringRedisTemplate.opsForValue().set(userId+":"+dId,json);

    }

    @GetMapping("/list")
    public R list(){
        List<ShoppingCart> list = shoppingCartService.listByUser();
        return R.success(list);
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart SC = shoppingCartService.sub(shoppingCart);
        return R.success(SC);
    }

    @DeleteMapping("/clean")
    public R clean(){
        shoppingCartService.clean();
        return R.success("success");
    }
}
